/**
 * 角色管理资源配置页面JS
 * User: fengchuang
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("MANAGE_ROLE_INFO");
    initSelect2();
    loadResourceTree();

    $("#userTab").click(function () {
        window.forward(springUrl + "/web/roleInfo/userConfigPage?uniqueNo="+$("#uniqueNo").val());
    });

    $("#headGoBackBtn").click(function(){
        window.forward(springUrl + "/web/roleInfo/index");
    });

    $("#expandAllBtn").click(function(){
        $.fn.zTree.getZTreeObj("resourceTree").expandAll(true);
        $("#expandAllBtn").addClass("hide");
        $("#foldAllBtn").removeClass("hide");
    });

    $("#foldAllBtn").click(function(){
        $.fn.zTree.getZTreeObj("resourceTree").expandAll(false);
        $("#expandAllBtn").removeClass("hide");
        $("#foldAllBtn").addClass("hide");
    });

    $("#saveBtn").click(function(){
        save();
    });
});

/**
 * 初始化菜单资源树
 */
function loadResourceTree(){

    var setting = {
        check: {
            enable: true,
            chkboxType: { "Y": "ps", "N": "ps" }
        },
        data: {
            key: {
                name: "resourceName"
            },
            simpleData: {
                enable: true,
                idKey: "resourceCode",
                pIdKey: "parentResourceCode"
            }
        }
    };

    var zNodes;

    function initTreeNode(data){
        zNodes = data;
        $.fn.zTree.init($("#resourceTree"), setting, zNodes);
        $.fn.zTree.getZTreeObj("resourceTree").expandAll(true);
    };

    jQuery.ajax({
        type:"post",
        cache:false,
        dataType:'json',
        url:springUrl + "/web/roleInfo/listResource",
        data:{
            roleNo: $("#uniqueNo").val()
        },
        success:function (data) {
            initTreeNode(data);
        },
        error : function(data){
            Alert.error("初始化页面数据失败!");
        }
    });
}

/**
 * 保存
 */
function save(){
    if($('#editForm').trigger("validate").isValid()){
        var index =  layer.msg('正在处理中...', {icon: 16, shade: 0.3, time: 0});
        var resourceNos = [];
        var treeObj = $.fn.zTree.getZTreeObj('resourceTree');
        var nodes = treeObj.getCheckedNodes(true);

        for (var i = 0; i < nodes.length; i++) {
            resourceNos.push(nodes[i].uniqueNo);
        }
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url:springUrl + "/web/roleInfo/saveRoleResourceRelation",
            data:{
                roleNo: $("#uniqueNo").val(),
                resourceNos : resourceNos
            },
            success:function (data) {
                layer.close(index);
                Alert.success("数据保存成功!", function(){
                    window.forward(springUrl+ "/web/roleInfo/index");
                });
            },
            error : function(data){
                layer.close(index);
                Alert.error("数据保存失败!");
            }
        });
    }
}